<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 头部 -->
        <!-- tree-node为需要渲染的数据  isroot为判定是否为子部门，子部门为false -->
        <treeTools :tree-node="company" :is-root="true" @delDepartment="getDepartmentList" @showDialog="showDialog" />
        <hr>
        <!-- 树形图 -->
        <el-tree :data="depts" :props="{label:'name'}" default-expand-all>
          <template #default="obj">
            <treeTools :tree-node="obj.data" @delDepartment="getDepartmentList" @showDialog="showDialog" @editDepts="editDepts" />
          </template>
        </el-tree>
      </el-card>
    </div>
    <addDept ref="addDept" :is-diolog-show.sync="showdia" :tree-node="treeNode" @addDepartment="getDepartmentList" />
  </div>
</template>

<script>
import treeTools from '@/views/departments/components/tree-tools.vue'
import { getDepartment } from '@/api/department'
import { listToTreeList } from '@/utils/index'
import addDept from '@/views/departments/components/add-dept.vue'
export default {
  components: { treeTools, addDept },
  data() {
    return {
      company: { name: '江苏传智播客教育科技股份有限公司', manager: '负责人', id: '' },
      depts: [],
      showdia: false,
      treeNode: {}
    }
  },
  created() {
    this.getDepartmentList()
  },
  methods: {
    async getDepartmentList() {
      const { depts } = await getDepartment()
      // console.log(depts)
      this.depts = listToTreeList(depts, '')
    },
    showDialog(treeNode) {
      this.showdia = true
      this.treeNode = treeNode
    },
    async editDepts(treeNode) {
      await this.$refs.addDept.editDepts(treeNode.id)
      this.treeNode = treeNode
      this.showdia = true
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
